package multithread.treadLocal.inheritablethreadLocal;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *每次数据重新设置一次也行，没看到数据重复的，当然最好是线程池用完了之后去释放线程数据；
 */
public class TestInheritableAndThreadPool3 {
    static final InheritableThreadLocal<String> threadParam = new InheritableThreadLocal<>();

    public static void main(String[] args) throws InterruptedException {
        //固定池内只有存活3个线程
        ExecutorService execService = Executors.newFixedThreadPool(3);
        //死循环几次才能看出效果
        while (true) {
            //线程1,里面有两个子线程
            Thread t = new Thread(()->{
                threadParam.set("t1");
                System.out.println("t1:" + threadParam.get());
            });
            execService.execute(t);

            Thread t2 = new Thread(()->{
                threadParam.set("t2");
                System.out.println("t2:" + threadParam.get());//
            });
            execService.execute(t2);

            Thread t3 = new Thread(()->{
                threadParam.set("t3");
                System.out.println("t3:" + threadParam.get());
            });
            execService.execute(t3);

            //线程4,线程1同级
            Thread t4 = new Thread(()-> {
                threadParam.set("t4");
                System.out.println("t4:" + threadParam.get());
            });
            execService.execute(t4);
        }
    }
}
